<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\DB;
use Laravel\Passport\HasApiTokens;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
use App\Notifications\ResetPasswordNotification;
use App\Common\ConstCode;
class Scene extends Authenticatable
{

    use HasApiTokens,Notifiable;


    protected $table = 'scene';
    protected $fillable = ['type','cover_uri','title','created_at','id','uid'];

    public    $timestamps = false;
    protected $primaryKey = 'id';

    public function lists($data,$uid){
        $category=$data['category'];
        $title=$data['title'];
        $where=[
            'type'=>ConstCode::YES,
            'status'=>ConstCode::YES,
            'is_shelf'=>ConstCode::YES,
        ];
        //我的包含私有
        $myCount=$this->count(['uid'=>$uid]);

        if($category) $where['category']=$category;
        if($title) $where[]=['title', 'LIKE', "%{$title}%"];

        $result=DB::table('scene')->where($where)
            ->select($this->fillable)
            ->forPage($data['page'], $data['pageSize'])
            ->orderBy(ConstCode::COLUMN, ConstCode::DIRECTION)
            ->get()
            ->toArray();
        if($result){
            foreach ($result as &$val){
                if($uid==$val->uid){
                    $val->is_my=1;
                }else{
                    $val->is_my=0;
                }
                $val->cover_uri=uri_fill($val->cover_uri);
                $val->classify=config('cartoontag.ALBUM_TYPE.场景库');
            }

        }
        $curCount = $this->count($where);
        $data = [
        'list'   =>  $result,
        'myCount'  =>  $myCount,
        'curCount' =>  $curCount,
        'myCollect'=>  0
        ];
        return $data;
    }

    public function count($where){
        $count=DB::table('scene')->where($where)->count();
        return $count;
    }

    public function my_lists($data,$uid){
        $title=$data['title'];
        $where=['uid'=>$uid];


        if($title) $where[]=['title', 'LIKE', "%{$title}%"];
        $result=DB::table('scene')->where($where)
            ->select('type','cover_uri','status','title','created_at','id')
            ->forPage($data['page'], $data['pageSize'])
            ->orderBy(ConstCode::COLUMN, ConstCode::DIRECTION)
            ->get()
            ->toArray();
        if($result){
            foreach ($result as &$val){
                $val->cover_uri=uri_fill($val->cover_uri);
            }
        }
        $curCount = $this->count($where);
        $data = [
            'list'   =>  $result,
            'total' =>  $curCount,
        ];
        return $data;
    }

    public function works_upload($data,$uid){
        $sources=$data['data'];
        $scene['uid']=$uid;
        $scene['type']=$data['is_open'];
        $scene['category']=$data['category'];
        $scene['title']=$data['title'];
        $scene['cover_uri']=$sources[0]['pic_uri'];
        $scene['category']=$data['category'];
        DB::transaction(function () use($scene,$sources) {
            //添加主表
            $id=DB::table('scene')->insertGetId($scene);
            foreach ($sources as $val){
                $picture['sid']=$id;
                $picture['pic_uri']=$val['pic_uri'];
                $picture['name']=$val['name'];
                $picture['type']=1;
                DB::table('cartoon_picture')->insert($picture);
            }
        });

        return true;
    }
}
